package view;

import game.*;
import controller.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;

/**
 * Listens for input, parses it and passed it to the controller if neccessary
 * @since 4.0
 * 
 * @author Trevor Bastian
 * @version 4.0
 */
public class GUIBridge implements ActionListener {
	private ChessController controller;
	private ChessGUI view;
	Main main;
	
	public GUIBridge (Main main) {
		this.main = main;
	}
	
	public void setController(ChessController controller) {
		this.controller = controller;
	}
	
	public void setView (ChessGUI view) {
		this.view = view;
	}
	
	@Override
	public void actionPerformed(ActionEvent event) {
		String command = event.getActionCommand();
		Object source = event.getSource();
		
		// check buttons
		if (source instanceof BoardButton) {
			BoardButton b = (BoardButton)source;
			controller.boardButtonClicked(b);
		}
		// check menus
		else if (command.equals("Exit")) {
			controller.quit();
		}
		else if(command.equals("Chess")) {
			controller.startGame(ClassicChess.class, view);
		}
		else if(command.equals("Knight's Tour")) {
			controller.startGame(KnightsTour.class, view);
		}
		else if (command.equals("8 Queen's Problem")) {
			controller.startGame(EightQueens.class, view);
		}
		else if (command.equals("Help")) {
			view.showHelp(controller.getGameState().gameType);
		}
		else if (command.equals("Save")) {
	    	controller.save();
		}
		else if (command.equals("Load")) {
			controller.load(view);
		}
		else if (command.equals("Undo")) {
			controller.undo(view);
		}
		else if (command.equals("Redo")) {
			controller.redo(view);
		}
		else {
			JOptionPane pane = new JOptionPane();
			pane.setSize(50, 10);
			JOptionPane.showMessageDialog(pane, "Invalid selection.");
		}
	}
}
